package com.ufgov.smartclient.component.table;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableCellEditor;

/**
 * 功能说明：带省略号按钮的单元格编辑器，单击...按钮可以弹出对话框，从对话框中选择指定值
 * 创建时间：2007-7-11
 * @author 刘永伟
 * @version 1.0
 */
public class EllipsisTableCellEditor extends AbstractCellEditor implements
  TableCellEditor {

  private Object returnValue = null;

  private JTable table = null;

  private int row;

  private int column;

  private boolean allowEdit = true;

  private EllipsisEditorPanel ellipsisPanel;

  private CellValueValidate cellValueValidate;

  private boolean stopEdit = false;

  public EllipsisTableCellEditor(final ValueSelect select) {
    this(select, true, null);
  }

  public EllipsisTableCellEditor(final ValueSelect select, final boolean allowEdit) {
    this(select, allowEdit, null);
  }

  public EllipsisTableCellEditor(final ValueSelect select, final boolean allowEdit,
    boolean stopEdit, CellValueValidate cellValueValidate) {
    this(select, allowEdit, cellValueValidate);
    this.stopEdit = stopEdit;
  }

  public EllipsisTableCellEditor(final ValueSelect select, final boolean allowEdit,
    CellValueValidate cellValueValidate) {
    this.cellValueValidate = cellValueValidate;
    this.allowEdit = allowEdit;
    ellipsisPanel = new EllipsisEditorPanel(allowEdit);
    ellipsisPanel.getTextComponent().getDocument().addDocumentListener(
      new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent e) {
          returnValue = ellipsisPanel.getTextComponent().getText();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
          returnValue = ellipsisPanel.getTextComponent().getText();
        }

        public void removeUpdate(DocumentEvent e) {
          returnValue = ellipsisPanel.getTextComponent().getText();
        };
      });
    ellipsisPanel.getButton().addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        returnValue = select.executeSelect(returnValue, row, column, table,
          ellipsisPanel.getButton().getLocationOnScreen());
        if (allowEdit) {
          ellipsisPanel.getTextComponent().setText(convertValueToText(returnValue));
        }
        if (stopEdit && table.isEditing())
          table.getCellEditor().stopCellEditing();
      }
    });
  }

  protected String convertValueToText(Object value) {
    return value == null ? "" : value.toString();
  }

  public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int rowIndex, int vColIndex) {
    this.table = table;
    this.row = rowIndex;
    this.column = vColIndex;
    //SwingUtilities.updateComponentTreeUI(ellipsisPanel);
    final JTextField textComponent = ellipsisPanel.getTextComponent();
    if (isSelected) {
      textComponent.setForeground(table.getSelectionForeground());
      ellipsisPanel.setBackground(table.getSelectionBackground());
    } else {
      //      ellipsisPanel.setForeground(table.getForeground());
      //      ellipsisPanel.setBackground(table.getBackground());
    }

    Border border = null;
    if (isSelected) {
      border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
    }
    if (border == null) {
      border = UIManager.getBorder("Table.focusCellHighlightBorder");
    }
    ellipsisPanel.setBorder(border);

    if (!isSelected && table.isCellEditable(rowIndex, vColIndex)) {
      Color col;
      col = UIManager.getColor("Table.focusCellForeground");
      if (col != null) {
        ellipsisPanel.setForeground(col);
      }
      col = UIManager.getColor("Table.focusCellBackground");
      if (col != null) {
        ellipsisPanel.setBackground(col);
      }
    }
    textComponent.setText(convertValueToText(value));
    returnValue = value;
    SwingUtilities.updateComponentTreeUI(ellipsisPanel);
    if (allowEdit) {
      //textComponent.setFocusable(true);
      textComponent.selectAll();
      textComponent.requestFocus();
    } else {
      //ellipsisPanel.getButton().requestFocus();
    }
    return ellipsisPanel;
  }

  @Override
  public boolean shouldSelectCell(EventObject e) {
    return true;
  }

  public CellValueValidate getCellValueValidate() {
    return cellValueValidate;
  }

  public void setCellValueValidate(CellValueValidate cellValueValidate) {
    this.cellValueValidate = cellValueValidate;
  }

  @Override
  public void cancelCellEditing() {
    super.cancelCellEditing();
  }

  @Override
  public boolean stopCellEditing() {
    if (cellValueValidate != null) {
      String message = cellValueValidate.validate(returnValue);
      if (message != null && !message.trim().equals("")) {
        JOptionPane.showMessageDialog(ellipsisPanel, message, "错误",
          JOptionPane.ERROR_MESSAGE);
        return false;
      }
    }
    return super.stopCellEditing();
  }

  public Object getCellEditorValue() {
    return returnValue;
  }

  public boolean isAllowEdit() {
    return allowEdit;
  }

  @Override
  public boolean isCellEditable(EventObject evt) {
    return true;
  }
}
